package pr;


public class Tree<E extends Comparable<E>> {
    private Node root;
    private int size;

    private class Node {
        Node left;
        Node right;
        E data;

        Node(E e) {
            data = e;
        }

       

        public boolean addChild(E e) {
            int val=data.compareTo(e);
            if (val==0) {
                return false;
            }else if (val>0) {
                if (left==null) {
                    left=new Node(e);
                    size++;
                    return true;
                }else {
                    return left.addChild(e);
                }
            }else {
                if (right==null) {
                    right=new Node(e);
                    size++;
                    return true;
                }else {
                    return right.addChild(e);
                }
            }
            
        }



        public void appendTo(StringBuilder bu) {
            if (left!=null) {
                left.appendTo(bu);
            }
            bu.append(",").append(data);
            if (right!=null) {
                right.appendTo(bu);
            }
        }
    }
    public boolean add(E e) {
        if (root == null) {
            root = new Node(e);
            size++;
            return true;
        } else {
            return root.addChild(e);
        }
    }
    
    public String toString() {
        if (root ==null) {
            return "[]";
        }
        StringBuilder bu=new StringBuilder("[");
        root.appendTo(bu);
        return bu.append("]").toString();
    }

    public static void main(String[] args) {
        Tree<Integer> tree=new Tree<>();
        tree.add(100);
        tree.add(200);
        tree.add(140);
        tree.add(190);
        System.out.println(tree.toString());
        
        
    }

}
